package com.java.escape.springproblem.applicationcontext;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.stereotype.Component;

/**
 * @ClassName UseListener
 * @Description 第二种方式，实现ApplicationListener接口（观察者模式）获取应用上下文
 * @Author wyp
 * @Date 2023/9/12 10:02
 * @Version 1.0
 */
@Slf4j
//@Component
public class UseListener implements ApplicationListener<ApplicationContextEvent> {
    @Override
    public void onApplicationEvent(ApplicationContextEvent event) {
        assert ApplicationContextStore.getApplicationContext() == null;
        ApplicationContextStore.setApplicationContext(event.getApplicationContext());
        assert ApplicationContextStore.getApplicationContext() != null;
        log.info("UseListener Done");
    }
}
